import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression

lines = np.loadtxt('../data/lr_dataset.csv', delimiter=',', dtype=float)
x_total = lines[:, 0:2]
y_total = lines[:, 2]
plt.scatter(x_total[y_total == 0, 0], x_total[y_total == 0, 1], c='r', marker='o', s=10)
plt.scatter(x_total[y_total == 1, 0], x_total[y_total == 1, 1], c='b', marker='x', s=10)
plt.show()

# 划分训练集和测试集，随机的打乱
np.random.seed(0)
ratio = 0.7
split = int(len(lines) * ratio)
idx = np.random.permutation(len(x_total))
x_total = x_total[idx]
y_total = y_total[idx]
x_train, y_train = x_total[:split], y_total[:split]
x_test, y_test = x_total[split:], y_total[split:]

lr_clf = LogisticRegression()
lr_clf.fit(x_train, y_train)
print("回归系数：",lr_clf.coef_[0],lr_clf.intercept_)

y_pred = lr_clf.predict(x_test)
print("准确率：",np.mean(y_pred == y_test))


